package es.xpt.ysf.commons.person.view;

import java.util.List;
import es.xpt.ysf.YSFException;
import es.xpt.ysf.commons.person.model.Person;
import es.xpt.ysf.logging.LogManager;
import es.xpt.ysf.model.Item;
import es.xpt.ysf.view.Controller;

@SuppressWarnings("serial")
public class PersonList extends Controller { // NO_UCD

	protected String entity;
	private String entityId;
	
	protected List<Person> people;
	private List<Person> selectionList;
	private int selectedIndex;
	protected Person person;
	
	public PersonHelper helper= new PersonHelper();
	
//	protected boolean editMode = false;
	

	public PersonList() {
		super();
		setMessageBundle("es.xpt.ysf.commons.person.Messages");
		person = new Person();
		person.setRole(new Item());
	}
	
	public PersonList(String entity) {
		this();
		this.entity = entity;
	}
	
	public PersonList(String entity, String entityId) {
		this(entity);
		this.entityId = entityId;
	}
	
	
	public List<Person> getPeople() {
		if ( people == null && entityId != null) {
			try {
				people = new PersonDelegate(getUserBean()).getEntityPeople(entity, entityId);
			} catch (YSFException e) {
				LogManager.getLog(getClass()).error(e.getMessage(), e);
			}
		}
		return people;
	}

	public List<Person> getPersonList(String fragment) {
		try {
			selectionList = new PersonDelegate(getUserBean()).searchPerson(fragment);
			int i = 1;
			for ( Person p: selectionList )
				p.setSequenceNumber(i++);
			return selectionList;
		} catch (YSFException e) {
			LogManager.getLog(getClass()).error(e.getMessage(), e);
			return null;
		}   
    }
	
	public String updatePerson() {
		try {
			new PersonDelegate(getUserBean()).updateEntityPerson(person);
		} catch (YSFException e) {
			super.setErrorMessage(e);
			LogManager.getLog(getClass()).error(e.getMessage(), e);
			return "failure";
		}
		super.setInfoMessage("commons.person.update");
		people = null;
		return "sucess";
	}

	public String savePerson() {
		try {
			  new PersonDelegate(getUserBean()).insertEntityPerson(person, entity, entityId);			  
			} catch (YSFException e) {
				super.setErrorMessage(e);
				LogManager.getLog(getClass()).error(e.getMessage(), e);
				return "failure";
			}
			super.setInfoMessage("commons.person.save");
			people = null;
			return "sucess";
	}
	
	public String cancelEdit() {
		helper.setEditMode(false);
		people = null;
		return "success";
	}
	
	public String insertPerson() {
		try {
			  new PersonDelegate(getUserBean()).insertEntityPerson(person,entity,entityId);
			} catch (YSFException e) {
				super.setErrorMessage(e);
				LogManager.getLog(getClass()).error(e.getMessage(), e);
				return "failure";
			}
			super.setInfoMessage("commons.person.insert");
			people = null;
		return "sucess";
	}
	
	public String addPerson() {
		try {
			person.setId(selectionList.get(selectedIndex-1).getId());
			  new PersonDelegate(getUserBean()).addEntityPerson(entityId, entity, person.getId(),person.getRole().getCode());
			} catch (YSFException e) {
				super.setErrorMessage(e);
				LogManager.getLog(getClass()).error(e.getMessage(), e);
				return "failure";
			}
		super.setInfoMessage("commons.person.add");
		people = null;
		return "sucess";
	}

	public String removePerson() {
		person = (Person)helper.getHtmlDataTable().getRowData();
		try {
			  new PersonDelegate(getUserBean()).removeEntityPerson(entityId, entity, person.getId(),person.getRole().getCode());
			} catch (YSFException e) {
				super.setErrorMessage(e);
				LogManager.getLog(getClass()).error(e.getMessage(), e);
				return "failure";
			}
		super.setInfoMessage("commons.person.remove");
		people = null;
		return "sucess";
	}
	
	public String editPerson() {
		helper.setEditMode(true);
		person = (Person)helper.getHtmlDataTable().getRowData();
		return "success";
	}
	

	

// GETTERS and SETTERS
	
	public String getEntity() {
		return entity;
	}

	public void setEntity(String entity) {
		this.entity = entity;
	}

	public String getEntityId() {
		return entityId;
	}

	public void setEntityId(String entityId) {
		this.entityId = entityId;
	}

	public void setPeople(List<Person> people) {
		this.people = people;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public int getSelectedIndex() {
		return selectedIndex;
	}

	public void setSelectedIndex(int selectedIndex) {
		this.selectedIndex = selectedIndex;
	}

	public PersonHelper getHelper() {
		return helper;
	}

	public void setHelper(PersonHelper helper) {
		this.helper = helper;
	}
}
